package com.huike.pmps.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huike.pmps.model.dto.member.MemberRoleDTO;
import com.huike.pmps.model.dto.role.RolePermissionDTO;
import com.huike.pmps.model.entity.Member;
import com.huike.pmps.model.entity.Role;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface RoleDao extends BaseMapper<Role> {

    /**
     * 根据用户id查找角色列表
     *
     * @param memberId 用户id
     * @return 角色列表
     */
    @Select("select r.* from es_member_role mr,es_role r where mr.role_id = r.id and r.state=1 and mr.member_id = #{memberId}")
    @Results(id = "roleMap", value = {
            @Result(id = true, property = "id", column = "id"),
            @Result(property = "roleName", column = "role_name"),
            @Result(property = "name", column = "name"),
            @Result(property = "state", column = "state"),
            @Result(property = "createTime", column = "create_time"),
            @Result(property = "permissions", column = "id", many = @Many(select = "com.huike.pmps.dao.PermissionDao.findByRoleId", fetchType = FetchType.EAGER))
    })
    List<Role> findByMemberId(Integer memberId);


    /**
     * 根据角色roleId，删除(角色权限关系表)中的数据
     *
     * @param roleId
     * @return
     */
    @Delete(" delete from es_role_permission where role_id= #{roleId} ")
    int deleteRoleAndPermissionRelationByRoleId(Integer roleId);


    @Insert({
            "<script>",
            "insert into es_role_permission(role_id, permission_id) values ",
            "<foreach collection='list' item='item' index='index' separator=','>",
            "(#{item.roleId}, #{item.permissionId})",
            "</foreach>",
            "</script>"
    })
    int insertRoleAndPermissionRelation( List<RolePermissionDTO> list);
}
